package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.DisplayMetrics;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by Lenovo on 2018/4/19.
 */

public class DrawActivity extends Activity implements GestureDetector.OnGestureListener{

    DrawView drawView;
    GestureDetector detector ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
        drawView = new DrawView(this,displayMetrics.widthPixels,displayMetrics.heightPixels);
        setContentView(drawView);
        detector = new GestureDetector(this,this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
    @Override
    public boolean onDown(MotionEvent e) {
        //  path.moveTo(x,y);
  /*      if(!drawView.start) {
            float x = e.getX();
            float y = e.getY();
            drawView.path.moveTo(x,y);
            drawView.preX = x;
            drawView.preY = y;
            drawView.start=true;
        }*/
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
   /*     float x = e.getX();
        float y = e.getY();
        drawView.path.lineTo(x,y);
        drawView.preX = x;
        drawView.preY = y;
        drawView.invalidate();*/
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
   /*     float x = e.getX();
        float y = e.getY();
        drawView.path.lineTo(x,y);
        drawView.cacheCanvas.drawPath(drawView.path,drawView.paint);
        drawView.path.reset();
        drawView.start=false;
        drawView.invalidate();*/
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false;
    }


    class  DrawView extends View implements GestureDetector.OnGestureListener{
        //first point
        boolean start = false;

        //定义记录前一个点击事件发生点的坐标
        float preX,preY;
        private Path path;
        public Paint paint = null;
        Bitmap cacheBitmap = null;
        Canvas cacheCanvas = null;
        GestureDetectorCompat gestureDetector ;

        public DrawView(Context context,int width,int height){
            super(context);
            cacheBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
            cacheCanvas = new Canvas();
            path = new Path();
            cacheCanvas.setBitmap(cacheBitmap);
            paint = new Paint(Paint.DITHER_FLAG);
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(1);
            paint.setAntiAlias(true);
            paint.setDither(true);

            gestureDetector= new GestureDetectorCompat(context,this);

            setLongClickable(true);
            this.setOnTouchListener(new OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    return gestureDetector.onTouchEvent(event);
                }
            });

    }

       /* @Override
        public boolean onTouchEvent(MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
*/
        @Override
        protected void onDraw(Canvas canvas) {
            Paint bmpPaint = new Paint();
            canvas.drawBitmap(cacheBitmap,0,0,bmpPaint);
            canvas.drawPath(path,paint);
        }

        @Override
        public boolean onDown(MotionEvent e) {
            if(!start) {
                float x = e.getX();
                float y = e.getY();
                path.moveTo(x,y);
                preX = x;
                preY = y;
                start=true;
            }
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
            path.lineTo(x,y);
            preX = x;
            preY = y;
            invalidate();
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            float x = e.getX();
            float y = e.getY();
            path.lineTo(x,y);
            cacheCanvas.drawPath(drawView.path,drawView.paint);
            path.reset();
            start=false;
            invalidate();
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }
    }


}
